package ca.newcommerce.youtube.webservice
{
    import ca.newcommerce.youtube.data.*;
    import ca.newcommerce.youtube.events.*;
    import ca.newcommerce.youtube.feeds.*;
    import com.adobe.serialization.json.*;
    import com.carlcalderon.arthropod.*;
    import flash.events.*;
    import flash.net.*;

    public class YouTubeClient extends EventDispatcher
    {
        protected var _requestId:Number = 0;
        protected var _requestQueue:Array;
        public static const ORDER_BY_RELEVANCE:String = "relevance";
        public static const STD_MOST_VIEWED:String = "most_viewed";
        public static const TIME_ALL:String = "all_time";
        public static const ORDER_BY_PUBLISHED:String = "published";
        public static const STD_MOBILE_VIDEOS:String = "watch_on_mobile";
        public static const STD_TOP_RATED:String = "top_rated";
        public static const STD_MOST_DISCUSSED:String = "most_discussed";
        public static const STD_MOST_LINKED:String = "most_linked";
        public static const STD_MOST_RESPONSED:String = "most_responded";
        public static const TIME_WEEK:String = "this_week";
        public static const RACY_EXCLUDE:String = "exclude";
        public static const TIME_MONTH:String = "this_month";
        public static const TIME_TODAY:String = "today";
        public static const STD_RECENTLY_FEATURED:String = "recently_featured";
        public static const STD_TOP_FAVORITES:String = "top_favorites";
        public static const ORDER_BY_VIEWCOUNT:String = "viewCount";
        private static var _instance:YouTubeClient;
        public static const RACY_INCLUDE:String = "include";

        public function YouTubeClient()
        {
            this._requestQueue = [];
            return;
        }// end function

        protected function doProfileLoaded(event:Event) : void
        {
            Debug.log("doProfileLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new ProfileData(JSON.decode(event.target.data as String).entry);
            dispatchEvent(new ProfileEvent(ProfileEvent.PROFILE_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        protected function doCommentsLoaded(event:Event) : void
        {
            Debug.log("::: doCommentsLoaded :::");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new CommentFeed(event.target.data as String);
            dispatchEvent(new CommentFeedEvent(CommentFeedEvent.COMMENT_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        public function getPlaylist(param1:String, param2:Number = 1, param3:Number = 50) : Number
        {
            var _loc_4:* = new URLRequest("http://gdata.youtube.com/feeds/api/playlists/" + param1 + "?alt=json&start-index=" + param2 + "&max-results=" + param3);
            return this.runLoader(_loc_4, this.doPlaylistLoaded, {comment:"playlist", playlistId:param1});
        }// end function

        protected function getLoaderIndex(param1:URLLoader) : Number
        {
            var _loc_2:Number = 0;
            while (_loc_2 < this._requestQueue.length)
            {
                
                if (this._requestQueue[_loc_2].loader == param1)
                {
                    return _loc_2;
                }
                _loc_2 = _loc_2 + 1;
            }
            return -1;
        }// end function

        public function getVideoResponses(param1:String) : Number
        {
            var _loc_2:* = new URLRequest("http://gdata.youtube.com/feeds/api/videos/" + param1 + "/responses?alt=json");
            return this.runLoader(_loc_2, this.doResponsesLoaded, {comment:"responses", videoId:param1});
        }// end function

        protected function doUserContactsLoaded(event:Event) : void
        {
            Debug.log("doUserContactsLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new ContactFeed(event.target.data as String);
            dispatchEvent(new ContactFeedEvent(ContactFeedEvent.USER_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        protected function doSubscriptionsLoaded(event:Event) : void
        {
            Debug.log("doSubscriptionsLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new SubscriptionFeed(event.target.data as String);
            dispatchEvent(new SubscriptionFeedEvent(SubscriptionFeedEvent.SUBSCRIPTION_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        public function getVideos(param1:String = "", param2:Number = 1, param3:Number = 50, param4:String = "all", param5:String = "relevance", param6:String = "exclude", param7:String = "", param8:Array = null, param9:Array = null, param10:Array = null, param11:Array = null) : Number
        {
            var _loc_12:String = "http://gdata.youtube.com/feeds/api/videos";
            if (param8 is Array && param8.length > 0)
            {
                _loc_12 = _loc_12 + ("/-/" + param8.join("%7C"));
            }
            if (param10 is Array && param10.length > 0)
            {
                if (_loc_12.indexOf("/-/") == -1)
                {
                    _loc_12 = _loc_12 + "/-";
                }
                _loc_12 = _loc_12 + ("/" + param10.join("%7C"));
            }
            _loc_12 = _loc_12 + ("?alt=json&orderby=" + param5 + "&racy=" + param6 + "&start-index=" + param2 + "&max-results=" + param3);
            if (param4 != "all")
            {
                _loc_12 = _loc_12 + ("&lr=" + param4);
            }
            if (param1.length > 0)
            {
                _loc_12 = _loc_12 + ("&vq=" + param1);
            }
            if (param7.length > 0)
            {
                _loc_12 = _loc_12 + ("&author=" + param7);
            }
            var _loc_13:* = new URLRequest(_loc_12);
            return this.runLoader(_loc_13, this.doVideosLoaded, {comment:"videos", search:param1, author:param7, categories:param8, keywords:param10, orderBy:param5, racy:param6, startIndex:param2, maxResults:param3});
        }// end function

        public function getUserFavorites(param1:String, param2:Array = null, param3:Array = null, param4:Number = 1, param5:Number = 50) : Number
        {
            var _loc_6:* = "http://gdata.youtube.com/feeds/api/users/" + param1 + "/favorites";
            if (param2 is Array && param2.length > 0)
            {
                _loc_6 = _loc_6 + ("/-/" + param2.join("%7C"));
            }
            if (param3 is Array && param3.length > 0)
            {
                if (_loc_6.indexOf("/-/") == -1)
                {
                    _loc_6 = _loc_6 + "/-";
                }
                _loc_6 = _loc_6 + ("/" + param3.join("%7C"));
            }
            _loc_6 = _loc_6 + ("?alt=json&start-index=" + param4 + "&max-results=" + param5);
            var _loc_7:* = new URLRequest(_loc_6);
            return this.runLoader(_loc_7, this.doUserFavoritesLoaded, {comment:"favorites", username:param1});
        }// end function

        protected function doIOError(event:IOErrorEvent) : void
        {
            Debug.log("WSClient.doIOError(" + event.toString());
            dispatchEvent(new YouTubeErrorEvent(YouTubeErrorEvent.IO_ERROR, true, false, event.toString()));
            return;
        }// end function

        public function getVideoComments(param1:String, param2:Number = 1, param3:Number = 50) : Number
        {
            var _loc_4:* = new URLRequest("http://gdata.youtube.com/feeds/api/videos/" + param1 + "/comments?alt=json&start-index=" + param2 + "&max-results=" + param3);
            return this.runLoader(_loc_4, this.doCommentsLoaded, {comment:"comments", videoId:param1});
        }// end function

        protected function handleIOError(event:IOErrorEvent) : void
        {
            Debug.log("ioError on loader:" + this.getLoaderIndex(event.target as URLLoader));
            return;
        }// end function

        protected function doConnectionStatus(event:NetStatusEvent) : void
        {
            return;
        }// end function

        public function getUserPlaylists(param1:String, param2:Number = 1, param3:Number = 50) : Number
        {
            var _loc_4:* = new URLRequest("http://gdata.youtube.com/feeds/api/users/" + param1 + "/playlists?alt=json&start-index=" + param2 + "&max-results=" + param3);
            return this.runLoader(_loc_4, this.doUserPlaylistsLoaded, {comment:"user_playlist", username:param1});
        }// end function

        protected function doPlaylistLoaded(event:Event) : void
        {
            Debug.log("doPlaylistLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data as String);
            dispatchEvent(new VideoFeedEvent(VideoFeedEvent.VIDEO_PLAYLIST_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        protected function getWrapper(param1:URLLoader) : Object
        {
            return this._requestQueue[this.getLoaderIndex(param1)];
        }// end function

        protected function doUserFavoritesLoaded(event:Event) : void
        {
            Debug.log("doUserFavoritesLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data as String);
            dispatchEvent(new VideoFeedEvent(VideoFeedEvent.USER_FAVORITES_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        public function rawUrl(param1:String) : Number
        {
            var _loc_2:* = new URLRequest(param1);
            return this.runLoader(_loc_2, this.doRawUrlLoaded, {comment:"rawUrl", url:param1});
        }// end function

        public function getUserProfile(param1:String) : Number
        {
            var _loc_2:* = new URLRequest("http://gdata.youtube.com/feeds/api/users/" + param1 + "?alt=json");
            return this.runLoader(_loc_2, this.doProfileLoaded, {comment:"profile", username:param1});
        }// end function

        public function doResponsesLoaded(event:Event) : void
        {
            Debug.log("doResponsesLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data as String);
            dispatchEvent(new ResponseFeedEvent(ResponseFeedEvent.RESPONSES_DATA_RECEIVED, _loc_2.id, _loc_2.videoId, _loc_3));
            return;
        }// end function

        public function getRelatedVideos(param1:String, param2:Number = 1, param3:Number = 50) : Number
        {
            var _loc_4:* = new URLRequest("http://gdata.youtube.com/feeds/api/videos/" + param1 + "/related?alt=json&start-index=" + param2 + "&max-results=" + param3);
            return this.runLoader(_loc_4, this.doRelatedVideosLoaded, {comments:"related_videos", videoId:param1});
        }// end function

        protected function doVideosLoaded(event:Event) : void
        {
            Debug.log("doVideosLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data);
            dispatchEvent(new VideoFeedEvent(VideoFeedEvent.VIDEO_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        protected function doUserPlaylistsLoaded(event:Event) : void
        {
            Debug.log("doUserPlaylistsLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new PlaylistFeed(event.target.data as String);
            dispatchEvent(new PlaylistFeedEvent(PlaylistFeedEvent.PLAYLIST_DATA_RECEIVED, _loc_2.id, _loc_2.username, _loc_3));
            return;
        }// end function

        public function getStandardFeed(param1:String, param2:String = "", param3:Number = 1, param4:Number = 50) : Number
        {
            var _loc_5:* = "http://gdata.youtube.com/feeds/api/standardfeeds/" + param1 + "?alt=json&start-index=" + param3 + "&max-results=" + param4;
            if (param1 == STD_TOP_RATED && param1 == STD_MOST_VIEWED && param2.length > 0)
            {
                _loc_5 = _loc_5 + ("&time=" + param2);
            }
            var _loc_6:* = new URLRequest(_loc_5);
            return this.runLoader(_loc_6, this.doStdFeedLoaded, {comment:"standard_feed", type:param1, time:param2});
        }// end function

        public function getUserContacts(param1:String, param2:Array = null, param3:Number = 1, param4:Number = 50) : Number
        {
            var _loc_5:* = "http://gdata.youtube.com/feeds/api/users/" + param1 + "/contacts";
            if (param2 != null && param2.length > 0)
            {
                _loc_5 = _loc_5 + ("/-/" + param2.join("%7C"));
            }
            _loc_5 = _loc_5 + ("?alt=json&start-index=" + param3 + "&max-results=" + param4);
            var _loc_6:* = new URLRequest(_loc_5);
            return this.runLoader(_loc_6, this.doUserContactsLoaded, {comment:"contacts", username:param1, categories:param2, startIndex:param3, maxResults:param4});
        }// end function

        public function getUserSubscriptions(param1:String, param2:Array = null, param3:Number = 1, param4:Number = 50) : Number
        {
            var _loc_5:* = "http://gdata.youtube.com/feeds/api/users/" + param1 + "/subscriptions";
            if (param2 != null && param2.length > 0)
            {
                _loc_5 = _loc_5 + ("/-/" + param2.join("%7C"));
            }
            _loc_5 = _loc_5 + ("?alt=json&start-index=" + param3 + "&max-results=" + param4);
            var _loc_6:* = new URLRequest(_loc_5);
            return this.runLoader(_loc_6, this.doSubscriptionsLoaded, {comment:"subscriptions", username:param1});
        }// end function

        protected function doRawUrlLoaded(event:Event) : void
        {
            Debug.log("doRawUrlLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            dispatchEvent(new YouTubeEvent(YouTubeEvent.RAW_URL_DATA_RECEIVED, _loc_2, null));
            return;
        }// end function

        protected function doStdFeedLoaded(event:Event) : void
        {
            Debug.log("doStdFeedLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data as String);
            dispatchEvent(new StandardVideoFeedEvent(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED, _loc_2.id, _loc_2.type, _loc_3));
            return;
        }// end function

        protected function handleProgress(event:ProgressEvent) : void
        {
            var _loc_2:* = Math.round(event.bytesLoaded / event.bytesTotal * 100);
            var _loc_3:* = this.getLoaderIndex(event.target as URLLoader);
            return;
        }// end function

        protected function doRelatedVideosLoaded(event:Event) : void
        {
            Debug.log("doRelatedVideosLoaded");
            var _loc_2:* = this.getWrapper(event.target as URLLoader);
            var _loc_3:* = new VideoFeed(event.target.data as String);
            dispatchEvent(new VideoFeedEvent(VideoFeedEvent.RELATED_VIDEOS_DATA_RECEIVED, _loc_2.id, _loc_3));
            return;
        }// end function

        protected function runLoader(param1:URLRequest, param2:Function, param3:Object) : Number
        {
            var _loc_4:* = new URLLoader();
            new URLLoader().dataFormat = URLLoaderDataFormat.TEXT;
            _loc_4.addEventListener(NetStatusEvent.NET_STATUS, this.doConnectionStatus);
            _loc_4.addEventListener(IOErrorEvent.IO_ERROR, this.doIOError);
            _loc_4.addEventListener(ProgressEvent.PROGRESS, this.handleProgress);
            _loc_4.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.doSecurityError);
            _loc_4.addEventListener(Event.COMPLETE, param2);
            _loc_4.load(param1);
            var _loc_5:String = this;
            _loc_5._requestId = this._requestId + 1;
            param3.id = this._requestId + 1;
            param3.loader = _loc_4;
            this._requestQueue.push(param3);
            return (this._requestId - 1);
        }// end function

        protected function doSecurityError(event:SecurityErrorEvent) : void
        {
            Debug.log("WSClient.doSecurityError(" + event.toString());
            dispatchEvent(new YouTubeErrorEvent(YouTubeErrorEvent.SECURITY_ERROR, true, false, event.toString()));
            return;
        }// end function

        public static function fromURL(param1:String) : Number
        {
            return -1;
        }// end function

    }
}
